package school;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class RegSuccessServlet
 */
public class RegSuccessServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegSuccessServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// recuperer les session ids qui ont ete coches
		String[] checks = request.getParameterValues("tabCheckbox[]");
		
		JDBCOptionDAO jdbcOption = new JDBCOptionDAO();
		Set<Option> listCourses = null;
		Set<Option> listCoursesExi = null;
		int[] sessionIds = new int[checks.length];
		// enregistrer les sessions qui sont deja inscrits
		int[] sessionExiIds = new int[checks.length];
		// enregistrer les sessions qui viennent d'etre inscrits
		int[] sessionRegIds = new int[checks.length];
		HttpSession session = request.getSession();
		String id = session.getAttribute("clientID").toString();
		
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<body>");
		out.println("<br/>");
		out.println("If you want to manager your schedules, you can access your admin page by clicking here: ");
		out.println("<a href=\"http://localhost:8080/PROJET_LO54/PersonalCentre.jsp\">Manage Page<br/><br/></a>");
	
		if(id != null){
			int clientId = Integer.parseInt(id);
			int j = 0;
			int ind = 0;
			for(int i = 0; i < checks.length; i++){
				sessionIds[i] = Integer.parseInt(checks[i]);
				
				if(jdbcOption.sessionExistence(clientId, sessionIds[i])){		
					sessionExiIds[ind] = sessionIds[i];
					ind++;
				} else {
					jdbcOption.addSessionToClient(clientId, sessionIds[i]);
					sessionRegIds[j] = sessionIds[i];
					j++;
				}
		
			}
			
			out.println("<form name=\"myCourses\" method=\"GET\" action=\"homePage.jsp\">");
			
			if(ind != 0){
				listCoursesExi = jdbcOption.getListCoursesRegistered(sessionExiIds);
				out.println("<h2>You have already registered this course:</h2>");
				for(Option op: listCoursesExi){
					out.println(op.getCode() + "  "+ op.getTitle() + " (from " + op.getDate_start() + " to " + op.getDatae_end() + ")<br/>");		
				}
			}
			
			if(j != 0){
				listCourses = jdbcOption.getListCoursesRegistered(sessionRegIds);
				out.println("<h2>Congratulations! You have just registered these courses </h2>");
				
				out.println("<table col=\"4\" border=\"1\"><tr>");
				out.println("<th width=\"100\">Code</th><th width=\"150\">Title</th><th width=\"200\">Sessions</th><th width=\"100\">Location</th></tr>");
				
				for(Option o : listCourses) {
					out.println("<tr>");
					out.println("<td align=\"center\">" + o.getCode() + "</td>");
					out.println("<td align=\"center\">" + o.getTitle() + "</td>");
					out.println("<td align=\"center\">" + o.getDate_start() + " to " + o.getDatae_end() + "</td>");
					out.println("<td align=\"center\">" + o.getLocation() + "</td>");
					out.println("</tr>");
				}

				out.println("</table>");

			}	

		}
		else{
			out.println("<h2>You did not login in, please login in to register courses. </h2>");			
		}
		
		out.println("<br/>");
		out.println("<input type=\"submit\" value=\"Return to home page\"/>");
		out.println("</form>");
		out.println("</body>");
		out.println("</html>");
	}

}
